<div class="fsh-rightPanel">
    <div class="layui-anim layui-anim-upbit">
        <div class="layui-form-item wx-search" id="list_form">
            <div class="layui-inline">
                <input type="text" name="title" placeholder="请输入渠道名称" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn btn-primary" id="search">搜索</button>
            <div class="layui-inline right">
                <button class="layui-btn btn-primary" id="qrcode_add">新建渠道</button>
                <button class="layui-btn btn-danger" id="qrcode_del">批量删除</button>
            </div>
        </div>
        <table id="list_table" class="layui-hide" lay-filter="mainList"></table>
    </div>
</div>

<script>
    layui.use(['layer', 'table'], function () {
        var layer = layui.layer;
        var table = layui.table;


        var tableObj = table.render({
            id: 'list_table',
            elem: '#list_table',
            url: '/wxqrcode/listForPage',
            align: "center",
            cols: [[ //表头
                {type: 'checkbox'},
                {type: 'numbers', title: '序号',width:50},
                {field: 'title', title: '标题', width: 120, align: 'center'},
                {field: 'content', title: '内容',width: 220,  align: 'center'},
                {field: 'url', title: 'URL', align: 'center'},
                {
                    field: 'lock', title: '操作', width: 280, align: 'center', templet: function (d) {
                    return '<a class="font-primary2" lay-event="edit" href="javascript:;">修改</a>'+'<a href="javascript:;" class="font-primary" lay-event="create">生成二维码</a>'+'<a href="javascript:;" class="font-primary" lay-event="download">下载二维码</a>';
                }, unresize: true, align: 'center'
                }
            ]],
        });

        $("#search").click(function () {
            reloadTable(tableObj);
        });

        // 添加
        $("#qrcode_add").click(function () {
            showDialog({
                title: '添加'
                , template: 'add'
                , saveUrl: '/wxqrcode/update'
                , tableObj: tableObj
            })
        });

        // 删除
        $("#qrcode_del").click(function () {
            var data = table.checkStatus('list_table').data;//已选中数据
            if (data.length == 0) {
                layer.msg("至少选择一条");
                return;
            }
            var arr = [];     //选中数组
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].baseId)
            }
            showConfirm("确认删除？", function () {
                $.ajax({
                    url: '/wxqrcode/deleteById',
                    data: {"baseId": arr.join(",")},
                    success: function (result) {
                        if (result.success) {
                            layer.msg("删除成功");
                            reloadTable(tableObj);
                        } else {
                            layer.msg("删除失败");
                        }
                    }
                })
            });
        });


        //表格内部操作按钮监听
        table.on('tool(mainList)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            
            if (layEvent === 'edit') {
                showDialog({
                    title: '修改',
                    template: 'edit',
                    saveUrl: '/wxqrcode/update',
                    tableObj: tableObj,
                    htmlData: data,
                })
            }  
            if (layEvent === 'create') {

                $.ajax({
                    url: '/wxqrcode/reset',
                    data:{id:data.id},
                    success: function (result) {
                        if (result.success) {
                            layer.msg("生成成功");
                            reloadTable(tableObj);
                        }
                    },
                    error: function () {
                        layer.msg("生成异常");
                    }
                })
            }  
            if (layEvent === 'download') {
            	if(data.url && data.url!='')
	               window.open(data.url);
            	else layer.msg("二维码未生成");
            } 
        });

    });
</script>
